package ttelectronic.mobileholder.service;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import ttelectronic.mobileholder.common.MHCommon;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class MHPinInformationService extends Thread {

	// Thread parameters
	private OutputStream mOutputStream;
	private Handler mHandler;
	private int mBatteryLevel;
	private int mPinTemperature;

	public MHPinInformationService(Context context, BluetoothSocket socket,
			Handler handler) {
		mHandler = handler;
		context.getApplicationContext().registerReceiver(batteryLevelReceiver,
				new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
		try {
			mOutputStream = socket.getOutputStream();
			Log.d(MHCommon.TAG + " MHPinInformationService",
					"Create socket successfully");
		} catch (IOException e) {
			Log.d(MHCommon.TAG + " MHPinInformationService",
					"Can not create socket!");
		}
	}

	private BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
			int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
			int level = -1;
			if (rawlevel >= 0 && scale > 0) {
				level = (rawlevel * 100) / scale;
			}
			int tem = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
			mBatteryLevel = level;
			mPinTemperature = tem / 10;
		}
	};

	@Override
	public void run() {
		super.run();
		while (true) {
			try {
				Message msg = mHandler
						.obtainMessage(MHCommon.MESSAGE_BATERY_LEVEL);
				Bundle bundle = new Bundle();
				bundle.putString(MHCommon.BUNDLE_BATERY_LEVEL, mBatteryLevel
						+ "");
				msg.setData(bundle);
				mHandler.sendMessage(msg);

				String message = "p" + mBatteryLevel + mPinTemperature + "s";
				Log.d(MHCommon.TAG + "MHPinInformationService", message);

				PrintWriter pWriter = new PrintWriter(new OutputStreamWriter(
						mOutputStream));
				if (mBatteryLevel < MHCommon.PIN_CONFIGURATION) {
					pWriter.write(MHCommon.POSTMSG_PIN_EMPTY);
					pWriter.flush();
					Log.d(MHCommon.TAG + "PIN", "EMPTY");
				} else if (mBatteryLevel > 98) {
					pWriter.write(MHCommon.POSTMSG_PIN_FULL);
					pWriter.flush();
					Log.d(MHCommon.TAG + "PIN", "FULL");
				}

				if (mPinTemperature > MHCommon.TEM_CONFIGURATION) {
					pWriter.write(MHCommon.POSTMSG_TEM_OVER);
					pWriter.flush();
					Log.d(MHCommon.TAG + "PIN", "OVER");
				}
				Thread.sleep(60000);

			} catch (InterruptedException e) {
				Log.d(MHCommon.TAG + " MHPinInformationService",
						"Can not push message!");
			}
		}
	}

}
